package com.calabar.phm.etl.server;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

/**
 * @Author zmc <mingcheng.zhang@cdcalabar.com>
 * @Date 17-6-8 上午11:21
 * @Version v1.0
 * @Des 调用spark驱动器测试程序
 */
public class MainTest {

    public static void main(String[] args) {
        //驱动项目目录
        String projectDir = "/home/hdfs/etl-driver";
        //任务json字符串
        String json = "{\n" +
                "  \"jobId\":\"9999967\",\n" +
                "  \"subJobId\":\"321\",\n" +
                "  \"sparkEnv\": {},\n" +
                "  \"operators\": [\n" +
                "    {\n" +
                "      \"operatorUnique\": \"ijiji\",\n" +
                "      \"operatorType\": 0,\n" +
                "      \"operatorDeveloper\": \"段育平\",\n" +
                "      \"operatorName\": \"input_hdfs\",\n" +
                "      \"operatorClassName\": \"com.calabar.phm.etl.operator.input.Hdfs\",\n" +
                "      \"operatorPersist\":\"\",\n" +
                "      \"operatorParams\":\"{\\\\\"hdfsPath\\\\\": \\\\\"hdfs://hadoop-176.phmcluster.calabar:8020/user/hdfs/people.json\\\\\"}\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"operatorUnique\": \"kjgjir\",\n" +
                "      \"operatorType\": 1,\n" +
                "      \"operatorDeveloper\": \"张明诚\",\n" +
                "      \"operatorName\": \"compute_orderby\",\n" +
                "      \"operatorClassName\": \"com.calabar.phm.etl.operator.compute.OrderBy\",\n" +
                "      \"operatorPersist\":\"persist1\",\n" +
                "      \"operatorParams\": \"{\\\\\"params\\\\\": [{\\\\\"columnName\\\\\": \\\\\"id\\\\\",\\\\\"order\\\\\": \\\\\"desc\\\\\"}]}\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"operatorUnique\": \"cesef\",\n" +
                "      \"operatorType\": 2,\n" +
                "      \"operatorDeveloper\": \"王远华\",\n" +
                "      \"operatorName\": \"output_show\",\n" +
                "      \"operatorClassName\": \"com.calabar.phm.etl.operator.output.Show\",\n" +
                "      \"operatorPersist\":\"\",\n" +
                "      \"operatorParams\":\"{}\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"connection\": {\n" +
                "    \"ijiji\": [\n" +
                "      \"\"\n" +
                "    ],\n" +
                "    \"kjgjir\": [\n" +
                "      \"ijiji\"\n" +
                "    ],\n" +
                "    \"cesef\": [\n" +
                "      \"kjgjir\"\n" +
                "    ]\n" +
                "  }\n" +
                "}\n";
        //任务ID
        String jobId = "9999967";
        //子任务ID
        String subJobId = "321";

        String[] commands = new String[]{ "bin/start.sh", jobId,subJobId,json};
        ProcessBuilder processBuilder = new ProcessBuilder(commands).directory(new File(projectDir));
        processBuilder.redirectErrorStream(true);
        printCommands(commands);
        Process process = null;
        try {
            process = processBuilder.start();
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    process.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line);
                sb.append("\n\r");
            }
            br.close();
            String runningLog = sb.toString();
            if (!runningLog.isEmpty()) {
                System.out.println(runningLog);
            }
            process.waitFor();
        } catch (Exception ex) {
            System.err.println(ex);
        }

    }

    private static void printCommands(String[] commands) {
        String cmd = "kjfdsk f";
        for (String c : commands) {
            if (!cmd.isEmpty()) {
                cmd = cmd + " " + c;
            } else {
                cmd = c;
            }
        }
        System.out.println("\nThe commonds:" + cmd);
    }

}
